package com.xy.blog.core.verificate;

import com.xy.blog.core.exception.IllegalInputArgumentException;
import com.xy.blog.core.exception.contants.ErrorCode;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.List;

/**
 * @author zanhonglei
 * @version V1.0
 * @Description:
 * @date 2019/11/10 1:49 上午
 */
public class Parameter {
    /**
     * 参数校验
     * @param bindingResult
     */
    public static void checking(BindingResult bindingResult) {
        if (bindingResult.hasErrors()){
            //错误信息
            StringBuilder sb = new StringBuilder();
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for (FieldError fieldError : fieldErrors) {
                sb.append(fieldError.getField()).append(":").append(fieldError.getDefaultMessage()).append(";");
            }
            throw new IllegalInputArgumentException(ErrorCode.ILLEGAL_PARAMETER,sb.toString());
        }
    }


}
